<template>
  <div class="wft-s-canvas">
    <canvas id="wft-s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>
<script setup name="SIdentify" lang='ts'>
import { onMounted, watch } from 'vue';

const props = withDefaults(defineProps<{
  // 默认注册码
  identifyCode?: string,
  // 字体最小值
  fontSizeMin?: number,
  // 字体最大值
  fontSizeMax?: number,
  // 验证码图片背景色最小值
  backgroundColorMin?: number,
  // 验证码图片背景色最大值
  backgroundColorMax?: number,
  // 背景干扰点最小值
  dotColorMin?: number,
  // 背景干扰点最大值
  dotColorMax?: number,
  // 是否透明背景
  isBgTransparent?: boolean,
  // 容器宽度
  contentWidth?: number,
  // 容器高度
  contentHeight?: number
}>(), {
  identifyCode: '1234',
  fontSizeMin: 25,
  fontSizeMax: 35,
  backgroundColorMin: 200,
  backgroundColorMax: 220,
  dotColorMin: 60,
  dotColorMax: 120,
  isBgTransparent: false,
  contentWidth: 120,
  contentHeight: 42
})

onMounted(() => {
  drawPic()
})

watch(() => props.identifyCode, () => {
  drawPic()
})

// 生成一个随机数
function randomNum(min: number, max: number) {
  return Math.floor(Math.random() * (max - min) + min)
}

// 生成一个随机的颜色
function randomColor(min: number, max: number) {
  let r = randomNum(min, max)
  let g = randomNum(min, max)
  let b = randomNum(min, max)
  return 'rgb(' + r + ',' + g + ',' + b + ')'
}

function drawPic () {
  let canvas = document.getElementById('wft-s-canvas') as HTMLCanvasElement
  let ctx = canvas.getContext('2d') as CanvasRenderingContext2D
  ctx.textBaseline = 'bottom'
  // 绘制背景
  if(props.isBgTransparent) {
    ctx.fillStyle = 'transparent'
  } else {
    ctx.fillStyle = randomColor(props.backgroundColorMin, props.backgroundColorMax)
  }
  ctx.clearRect(0, 0, props.contentWidth, props.contentHeight) // 清除上一次绘制
  ctx.fillRect(0, 0, props.contentWidth, props.contentHeight)
  // 绘制文字
  for (let i = 0; i < props.identifyCode.length; i++) {
    drawText(ctx, props.identifyCode[i], i)
  }
  drawLine(ctx)
  drawDot(ctx)
}

function drawText (ctx: CanvasRenderingContext2D, txt: string, i: number) {
  ctx.fillStyle = randomColor(50, 160) // 随机生成字体颜色
  ctx.font = randomNum(props.fontSizeMin, props.fontSizeMax) + 'px SimHei' // 随机生成字体大小
  let x = (i + 1) * (props.contentWidth / (props.identifyCode.length + 1))
  let y = randomNum(props.fontSizeMax, props.contentHeight - 5)
  var deg = randomNum(-30, 30)
  // 修改坐标原点和旋转角度
  ctx.translate(x, y)
  ctx.rotate(deg * Math.PI / 180)
  ctx.fillText(txt, 0, 0)
  // 恢复坐标原点和旋转角度
  ctx.rotate(-deg * Math.PI / 180)
  ctx.translate(-x, -y)
}

function drawLine (ctx: CanvasRenderingContext2D) {
  // 绘制干扰线
  for (let i = 0; i < 4; i++) {
    ctx.strokeStyle = randomColor(100, 200)
    ctx.beginPath()
    ctx.moveTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight))
    ctx.lineTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight))
    ctx.stroke()
  }
}

function drawDot (ctx: CanvasRenderingContext2D) {
  // 绘制干扰点
  for (let i = 0; i < 30; i++) {
    ctx.fillStyle = randomColor(0, 255)
    ctx.beginPath()
    ctx.arc(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight), 1, 0, 2 * Math.PI)
    ctx.fill()
  }
}
</script>